package com.ruoyi.web.controller.wx_driver;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.vqms.domain.VQMSDriver;
import com.ruoyi.vqms.domain.VQMSVisitRecordYuchai;
import com.ruoyi.vqms.domain.VQMSVisitRecordYuchi;
import com.ruoyi.vqms.service.IVQMSDriverService;
import com.ruoyi.vqms.service.IVQMSVisitRecordYuchaiService;
import com.ruoyi.vqms.service.IVQMSVisitRecordYuchiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/wx/yuchi")
public class VisitRecordYuchiController  extends BaseController {

    @Autowired
    private IVQMSVisitRecordYuchiService yuchiService;
    @Autowired
    private IVQMSDriverService driverService;
    @Autowired
    private IVQMSVisitRecordYuchaiService visitRecordYuchaiService;

    /**
     * 新增访问记录（玉驰外围仓库）
     */
    @Log(title = "新增访问记录（玉驰外围仓库）", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VQMSVisitRecordYuchi vqmsVisitRecordYuchi){
//        if (vqmsVisitRecordYuchi.getLicensePlate().equals("无")){
//            vqmsVisitRecordYuchi.setLicensePlate(null);
//        }
        // 如果没有访客（司机），则新增。否则更新访客（司机）
        VQMSDriver old=driverService.getByPhone(vqmsVisitRecordYuchi.getPhone());
        if(old == null){
            VQMSDriver vqmsDriver=new VQMSDriver();
            vqmsDriver.setName(vqmsVisitRecordYuchi.getDriverName());
            vqmsDriver.setPhone(vqmsVisitRecordYuchi.getPhone());
            vqmsDriver.setLicensePlate(vqmsVisitRecordYuchi.getLicensePlate());
            vqmsDriver.setCreateBy(vqmsVisitRecordYuchi.getDriverName());
            driverService.add(vqmsDriver);
            //新增之后重新查询，排除id为空的情况
            vqmsDriver = driverService.getByPhone(vqmsVisitRecordYuchi.getPhone());
            vqmsVisitRecordYuchi.setDriverId(vqmsDriver.getId());
        }else{
            old.setName(vqmsVisitRecordYuchi.getDriverName());
            old.setPhone(vqmsVisitRecordYuchi.getPhone());
            old.setLicensePlate(vqmsVisitRecordYuchi.getLicensePlate());
            old.setUpdateBy(vqmsVisitRecordYuchi.getDriverName());
            driverService.update(old);
            vqmsVisitRecordYuchi.setDriverId(old.getId());

        }
        vqmsVisitRecordYuchi.setConfirmedBy(1); //外围仓库默认本人登记
        vqmsVisitRecordYuchi.setRegistrationStatus(2); //外围仓库默认已登记
        vqmsVisitRecordYuchi.setConfirmedTime(new Date()); //确认时间
//        vqmsVisitRecordYuchi.setNotes();
        vqmsVisitRecordYuchi.setCreateBy(vqmsVisitRecordYuchi.getDriverName());
        vqmsVisitRecordYuchi.setCreateTime(new Date());
        int i = yuchiService.add(vqmsVisitRecordYuchi);
        if (i != 1){
            return AjaxResult.error("新增玉驰访客失败");
        }
        List<VQMSVisitRecordYuchi> yuchiList = yuchiService.selectList(vqmsVisitRecordYuchi);
        if (yuchiList.isEmpty()){
            logger.error("未找到新增之后的记录");
        } else {
            //用负数加玉驰访客记录ID作为玉驰玉柴访客记录关联
            VQMSVisitRecordYuchi recordYuchi = yuchiList.get(0);
            Long reservaId = -999 - recordYuchi.getId();
            recordYuchi.setReservationId(reservaId);       //确保不跟正常预约ID冲突
            vqmsVisitRecordYuchi.setReservationId(reservaId);
            int n = yuchiService.update(recordYuchi);
        }

        //复制到玉柴访客记录
        VQMSVisitRecordYuchai visitRecordYuchai = new VQMSVisitRecordYuchai();
        visitRecordYuchai.setFactoryId(vqmsVisitRecordYuchi.getFactoryId());
        visitRecordYuchai.setRegisterWarehouse(vqmsVisitRecordYuchi.getFactoryName());  //登记仓库
        visitRecordYuchai.setDriverId(vqmsVisitRecordYuchi.getDriverId());
        visitRecordYuchai.setDriverName(vqmsVisitRecordYuchi.getDriverName());
        visitRecordYuchai.setPhone(vqmsVisitRecordYuchi.getPhone());
        visitRecordYuchai.setLicensePlate(vqmsVisitRecordYuchi.getLicensePlate());
        visitRecordYuchai.setCreateBy(vqmsVisitRecordYuchi.getDriverName());
        visitRecordYuchai.setVisitReason(vqmsVisitRecordYuchi.getNotes());          //来访事由
        visitRecordYuchai.setWorkCompany(vqmsVisitRecordYuchi.getWorkCompany());    //本人单位
        visitRecordYuchai.setVisitCompany(vqmsVisitRecordYuchi.getVisitCompany());  //到访单位
        visitRecordYuchai.setVisitName(vqmsVisitRecordYuchi.getVisitName());        //对接人
        visitRecordYuchai.setMaterials(vqmsVisitRecordYuchi.getMaterials());        //物资
        visitRecordYuchai.setCreateTime(new Date());
        visitRecordYuchai.setRegistrationStatus(2); //外围仓库默认已登记
        visitRecordYuchai.setConfirmedBy(1);    //外围仓库默认本人登记
        visitRecordYuchai.setConfirmedTime(new Date()); //确认时间
        if (vqmsVisitRecordYuchi.getReservationId() != null){
            visitRecordYuchai.setReservationId(vqmsVisitRecordYuchi.getReservationId());
        }
        try {
            int a = visitRecordYuchaiService.add(visitRecordYuchai);
        } catch (Exception e){
            logger.error("生成玉柴访客记录失败："+e.toString());
        }
        return AjaxResult.success();
    }


    /**
     * 司机出厂登记
     * @param vqmsVisitRecordYuchi
     * @return
     */
    @Log(title = "更新出厂时间（玉驰）", businessType = BusinessType.INSERT)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody VQMSVisitRecordYuchi vqmsVisitRecordYuchi){
        VQMSVisitRecordYuchi visitRecordYuchi = yuchiService.getFactoryTime(vqmsVisitRecordYuchi.getPhone());
        if ((visitRecordYuchi == null)){
            return AjaxResult.success("301","未找到进场记录");
        }
        //更新最新一条记录出厂时间
        visitRecordYuchi.setFactoryTime(new Date());
        int i = yuchiService.update(visitRecordYuchi);
        //同步对应玉柴访客记录
        if (visitRecordYuchi.getReservationId() != null){
            VQMSVisitRecordYuchai visitRecordYuchai = visitRecordYuchaiService.getReservationId(visitRecordYuchi.getReservationId());
            if (visitRecordYuchai != null){
                visitRecordYuchai.setFactoryTime(new Date());
                int n = visitRecordYuchaiService.update(visitRecordYuchai);
                if (n != 1){
                    logger.error("玉驰扫码出厂同步玉柴记录-同步玉柴访客记录出厂时间失败");
                }
            } else {
                logger.error("玉驰扫码出厂同步玉柴记录-未找到玉柴访客记录");
            }
        }
        return AjaxResult.success("200","出厂登记成功");
    }

}
